package com.js.foodordersystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.js.foodordersystem.entity.Address;
import com.js.foodordersystem.service.AddressService;
import com.js.foodordersystem.util.Result;

import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 存储用户的多个配送地址(Address)表控制层
 *
 * @author makejava
 * @since 2025-01-09 10:51:19
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("address")
public class AddressController {
    /**
     * 服务对象
     */
    @Resource
    private AddressService addressService;

    /**
     * 分页查询
     *
     * @param address 筛选条件
     * @param limit   分页对象
     * @param page    分页对象
     * @return 查询结果
     */
    @GetMapping("findAll")
    public Result findAll(Address address, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return addressService.selectAll(address, limit, page);
    }

    /**
     * 根据用户id查询所有的地址
     * @author 史超杰
     * @date 2025/1/14 10:33
     */
    @GetMapping("getAllByUserId")
    public Result getAllByUserId(Long userId) {
        if (userId == null) {
            return Result.failure("userId不能为空");
        }

        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("created_at");
        queryWrapper.orderByDesc("is_default");
        return Result.success(this.addressService.list(queryWrapper));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    public Result getById(Long id) {
        return Result.success(this.addressService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param address 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Address address) {
        //判断是否为空
        if (address == null) {
            return Result.failure("参数不能为空");
        }
        Integer isDefault = address.getIsDefault();
        if (isDefault == 1){
            //修改其他的地址为非默认
            addressService.lambdaUpdate()
                    .set(Address::getIsDefault, 2)
                    .eq(Address::getUserId, address.getUserId())
                    .update();
        }

        if (this.addressService.save(address)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param address 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Address address) {
        //判断是否为空
        if (address == null) {
            return Result.failure("参数不能为空");
        }

        Integer isDefault = address.getIsDefault();
        if (isDefault == 1){
            //修改其他的地址为非默认
            addressService.lambdaUpdate()
                    .set(Address::getIsDefault, 2)
                    .eq(Address::getUserId, address.getUserId())
                    .update();
        }

        if (this.addressService.updateById(address)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @PostMapping("delete")
    public Result deleteById(@RequestBody JSONObject data) {
        //判断id是否为空
        Long id = data.getLong("addressId");
        if (id == null) {
            return Result.failure("id不能为空");
        }

        if (this.addressService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

}

